<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Services\OrderService;
use App\Http\Requests\OrderRequest;
use App\Models\Order;

class OrderController extends Controller
{
	public function __construct() {
		$this->middleware('order.check');
	}

	public function create()
	{
		$order_id = session('order_id');
		$order = $order_id ? Order::find($order_id) : [];
		return view('home.checkout.index', ['order' => $order]);
	}

	public function store(OrderRequest $request, OrderService $service)
	{
		$datas = $request->all();
		$order = $service->createOrder($datas);
		return redirect()->route('order.create')->with('order_id', $order->id);
	}
	
    public function index()
    {
    	$orders = Auth::user()->orders()->latest()->paginate();
    	return view('home.order.index', ['data' => $orders]);
    }
}
